package org.redr.widgetGUI.core;

import java.util.HashMap;
import java.util.Map;

import org.redr.widgetGUI.settings.WidgetSettings;
import org.redr.widgetGUI.settings.WidgetState;

import com.trolltech.qt.gui.QComboBox;
import com.trolltech.qt.gui.QLabel;

public class ComboBox extends WidgetState{
	
	QComboBox	comboBox;
	
	public QComboBox widget(){
		return comboBox;
	}
	
	public ComboBox(LayoutState s, WidgetSettings ws){
		super(s, ws);
		s.addWidget(new QLabel(_getSetting("label", "!!FIX ME label SHOULD BE SET")));
		comboBox = new QComboBox();
		s.addWidget(comboBox);
		_setComboBoxSettings();
	}
	
	private void _setComboBoxSettings(){
		HashMap<String, String> i = items("items");
		for (String s : i.values()){
			comboBox.addItem(s);
		}
		int index = Integer.parseInt(_getSetting("index", "-1"));
		if (index > -1){
			comboBox.setCurrentIndex(index);
		}
	}
	
	public String currentText(){
		_setSetting("index", String.valueOf(comboBox.currentIndex()));
		return comboBox.currentText();
	}
	
	public String currentId(){
		_setSetting("index", String.valueOf(comboBox.currentIndex()));
		HashMap<String, String> kv	=	items("items");
		return kv.keySet().toArray(new String[0])[comboBox.currentIndex()];
	}
	
	public void update(Map<String, String> names){
		String currentT = comboBox.currentText();
		comboBox.clear();
		settings.items("items").setItems(names);
		for (String s : names.values()){
			comboBox.addItem(s);
		}
		
		comboBox.setCurrentIndex(comboBox.findText(currentT));
		currentText(); // run this again so that the correct index will be set.
		
	}

	public void clear() {
		// TODO Auto-generated method stub
		
	}

	public void addItem(String string) {
		// TODO Auto-generated method stub
		
	}

	public void addItem(String k, String string) {
		// TODO Auto-generated method stub
		
	}

}
